﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Gola.VN.Scb.Web.Common.Models;
using Gola.VN.Scb.Web.Presentation.Helpers;
using Gola.VN.Scb.Web.Presentation.Models;

namespace Gola.VN.Scb.Web.Presentation.Controllers
{
    [HandleError]
    public class SearchController : Controller
    {
        [HttpPost]
        public JsonResult Search(string searchText, int maxResults)
        {
             //var result = ProductService.AllProducts.Where(p => p.Title.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Take(maxResults). //todo: ngocbv
            var result = MainService.GetInstant().ProductService.GetProducts().Where(
                p => p.Title.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Take(maxResults).
                Select(s => new UISearchResultModels { Name = s.Title, View = s.View }).ToList();
            result.AddRange(
                // CategoryService.AllCategories.Where(c => c.Name.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Take(maxResults). //todo: replaced by ngocbv
                MainService.GetInstant().CategoryService.GetCategoriesAsFlat().Where(
                c => c.Name.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Select(b => new UICategoryModels
                                        {
                                            Id = b.Id,
                                            Name = b.Name
                                        }).Take(maxResults).
                                        Select(s => new UISearchResultModels
                                                        {
                                                            Name = s.Name,
                                                            View = s.InnerLink
                                                        }).ToList());

            result = result.OrderBy(s => s.Name).Take(maxResults).ToList();

            return Json(result);//(MainService.GetInstant().SearchServiceNew.DoSearch(searchText, maxResults));
        }
    }
}